package com.jiawa.train.business.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.jiawa.train.business.domain.po.SkToken;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.util.Date;

/**
 * <p>
 * 秒杀令牌 Mapper 接口
 * </p>
 *
 * @author hrz
 * @since 2025-09-10
 */
public interface SkTokenMapper extends BaseMapper<SkToken> {

    @Update("update from sk_token set `count` = if(`count` &lt #{count}, 0 ,`count` - #{count})" +
            " where date = #{date} and train_code = #{trainCode} and `count` > 0")
    int decrease(@Param("date") Date date, @Param("trainCode") String trainCode, @Param("count") Integer count);

    @Select("select * from sk_token where train_code = #{trainCode} and date = #{date}")
    SkToken selectByTrainAndDate(@Param("trainCode") String trainCode, @Param("date") Date date);
}
